﻿namespace Homework4_1
{
    /// <summary>
    /// 重新用Linq,来生成10000个学生的数据，包括序号、姓名随机三个英文字母（首字母大写）、年龄20-60随机分布、生日的年份按照年龄生成、身高随机生成、性别男女。并把生成的数据存入D盘的txt中。
    /// </summary>
    /// <param name="args"></param>
    class Person
    {
        //访问字段，控制字段，get 获取属性的值，set 设置属性的值
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime BirthDate { get; set; }
        public double Height { get; set; }
        public string Gender { get; set; }
    }

    class Program
    {
        //用于生成随机数
        private static Random random = new Random();

        static void Main()
        {
            //使用Linq来生成数据
            var persons = Enumerable.Range(1, 10000).Select(i =>
            {
                var name = GenerateRandomName();
                var age = random.Next(20, 61);
                var birthYear = DateTime.Now.Year - age;
                var birthDate = new DateTime(birthYear, random.Next(1, 13), random.Next(1, 29));
                var height = random.NextDouble() * (200 - 140) + 140; // Height between 140 and 200 cm  
                var gender = random.Next(0, 2) == 0 ? "Male" : "Female";

                //返回输出的结果
                return new Person
                {
                    Id = i,
                    Name = name,
                    Age = age,
                    BirthDate = birthDate,
                    Height = height,
                    Gender = gender
                };
            }).ToList();

            //将10000组数据存入D盘Csharp2024的PersonData.txt中
            string filePath = @"D:\\CSharp2024\\PersonsData.txt";
            File.WriteAllLines(filePath, persons.Select(p =>
                $"{p.Id},{p.Name},{p.Age},{p.BirthDate:yyyy-MM-dd},{p.Height:F2},{p.Gender}"));

            //输出表明成功
            Console.WriteLine("Data has been written to " + filePath);
        }
        //一个函数来生成学生的名字
        private static string GenerateRandomName()
        {
            var consonants = "bcdfghjklmnpqrstvwxyz";
            var vowels = "aeiou";

            char firstLetter = char.ToUpper(consonants[random.Next(consonants.Length)]);
            char secondLetter = vowels[random.Next(vowels.Length)];
            char thirdLetter = consonants[random.Next(consonants.Length)];

            return $"{firstLetter}{secondLetter}{thirdLetter}";
        }
    }
}